﻿#include "WaitSwipCard.h"
#include "ui_waitswipcard.h"

WaitSwipCard::WaitSwipCard(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WaitSwipCard)
{
    ui->setupUi(this);
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    countdown = new CountDownTime(ui->frameMid);
    countdown->setGeometry(710, 10, 30, 40);
    countdown->mysetText("60");
    card_WaitSwipCard = mt.getCard();
    connect(countdown, SIGNAL(CoundDownTimeisOver()), this, SLOT(countdownOver()));
    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(CountDownStart(int)));
    connect(&mt,SIGNAL(sendData(QString,int,uint)),this,SLOT(recvData(QString,int,uint)));
}
void WaitSwipCard::recvData(QString data, int value, unsigned int restMoney)
{
    if(focus_gun_ == 0)
    {
        qDebug() << "WaitSwipCard页面接收到的卡号是: " << data << ";  "  << "卡内余额是: "<< restMoney;
        qDebug() << "WaitSwipCard页面读卡是否成功: "<< value;
        QString m_data;
        if(value == 0)
        {
            switch(CardFlag)
            {
                case ReferBindedAdministrationCard:
                    if(A1Card == data || A2Card == data)
                    {
                        stackLayout->setCurrentIndex(chargeCardManageInfoPage);
                        this->countdown->CountDownStop();
                    }
                    else
                    {
                        stackLayout->setCurrentIndex(SystemSetting4Page);
                        this->countdown->CountDownStop();
                    }
                break;
            case 1:
            case 2:
            case 3:
            case 4:
            case 5://BindA1 BindA2 BindB1 BindB2 BindB3
                for(int i=0; i<8; i++)
                {
                    acard.card_id[i] =  mt.getCardNum()[i];
                    temp_cardNum[CardFlag-1][i] = acard.card_id[i];
                }

                if(CardFlag >= 1 && CardFlag <=2)
                {
                    acard.card_type = 0x0A;
                    acard.card_order = CardFlag;
                }
                else if(CardFlag >= 3 && CardFlag <= 5)
                {
                    acard.card_type = 0x0B;
                    acard.card_order = CardFlag-2;
                }

                if( !protocol_updateCard(&acard, 1) )
                {
                    qDebug("绑定成功!\n");
                    emit BindisOk(1);
                }
                else
                {
                    qDebug("绑定失败!\n");
                    emit BindisOk(0);
                }
                stackLayout->setCurrentIndex(chargeCardManageInfoPage);
                this->countdown->CountDownStop();
                break;
            case UnBindA1:
                for(int i=0; i<8; i++)
                {
                    if(!A2Card.trimmed().isEmpty() && (data.trimmed() == A2Card.trimmed()))
                    {
                        acard.card_id[i] =  temp_cardNum[0][i];
                    }
                    else
                    {
                        acard.card_id[i] =  mt.getCardNum()[i];
                    }
                    m_data += QString("%1").arg(acard.card_id[i], 2, 16, QLatin1Char('0'));
                }
                if(!A1Card.trimmed().isEmpty() && (A1Card.trimmed() == m_data.trimmed()))
                {
                    if( !protocol_updateCard(&acard, 2))
                    {
                        printf("A1解绑成功\n");
                        for(int i=0 ;i<8; i++)
                        {
                            acard.card_id[i] = 0;
                            temp_cardNum[0][i] = 0;
                            mt.getCardNum()[i] = 0;
                        }
                        emit UnBindingSignal(3);
                    }
                    else
                    {
                        printf("A1解绑失败\n");
                        emit UnBindingSignal(2);
                    }
                }
                stackLayout->setCurrentIndex(chargeCardManageInfoPage);
                this->countdown->CountDownStop();
                break;
            case UnBindA2:
                for(int i=0 ;i<8; i++)
                {
                    if(!A1Card.trimmed().isEmpty() && ( data.trimmed() == A1Card.trimmed() ) )
                    {
                        acard.card_id[i] =  temp_cardNum[1][i];
                    }
                    else
                    {
                        acard.card_id[i] = mt.getCardNum()[i];
                    }

                    m_data += QString("%1").arg(acard.card_id[i], 2, 16, QLatin1Char('0'));
                }
                if(!A2Card.trimmed().isEmpty() && (A2Card.trimmed() == m_data.trimmed()))
                {
                    if( !protocol_updateCard(&acard, 2))
                    {
                        printf("A2解绑成功\n");
                        for(int i=0 ;i<8; i++)
                        {
                            acard.card_id[i] = 0;
                            temp_cardNum[1][i] = 0;
                            mt.getCardNum()[i] = 0;
                        }
                        emit UnBindingSignal(3);
                    }
                    else
                    {
                        printf("A1解绑失败\n");
                        emit UnBindingSignal(2);
                    }
                }
                stackLayout->setCurrentIndex(chargeCardManageInfoPage);
                this->countdown->CountDownStop();
                break;
            case UnBindB1:
            case UnBindB2:
            case UnBindB3://UnBindB1 UnBindB2 UnBindB3
                for(int i=0; i<8; i++)
                {
                    if( ((!A1Card.trimmed().isEmpty()) && (data.trimmed() == A1Card.trimmed())) || ((!A2Card.trimmed().isEmpty()) && data.trimmed() == A2Card.trimmed()))
                    {
                        acard.card_id[i] =  temp_cardNum[CardFlag-6][i];
                    }
                    else
                    {
                        acard.card_id[i] =  mt.getCardNum()[i];
                    }

                    m_data += QString("%1").arg(acard.card_id[i], 2, 16, QLatin1Char('0'));
                }
                if( (!UnBingCard.trimmed().isEmpty()) && ( m_data.trimmed() == UnBingCard.trimmed()) )
                {
                    if( !protocol_updateCard(&acard, 2))
                    {
                        qDebug("%d解绑成功\n",CardFlag);
                        for(int i=0 ;i<8; i++)
                        {
                            acard.card_id[i] = 0;
                            temp_cardNum[CardFlag-6][i] = 0;
                            mt.getCardNum()[i] = 0;
                        }
                        emit UnBindingSignal(3);
                    }
                    else
                    {
                        qDebug("%d解绑失败\n",CardFlag);
                        emit UnBindingSignal(2);
                    }
                }
                stackLayout->setCurrentIndex(chargeCardManageInfoPage);
                this->countdown->CountDownStop();
                break;
            }
        }
    }

}
void WaitSwipCard::ReferAdministerCardMod(int CardReferMod , QString A1, QString A2)
{
    CardFlag = CardReferMod;
    A1Card = A1;
    A2Card = A2;
}
void WaitSwipCard::CountDownStart(int current)
{
    if(current == WaitSwipCardPage)
    {
        countdown->CountDownStart();
        if( !mt.isRunning() && card_WaitSwipCard->errcode == 0 )
        {
            mt.start();
        }
    }
}

void WaitSwipCard::BindCardMode(int SwipFlag_Bind, QLineEdit *edit, QString A1, QString A2)
{
    CardFlag = SwipFlag_Bind;
    A1Card = A1;
    A2Card = A2;
}

void WaitSwipCard::UnBindCardMode(int SwipFlag_UnBind, QString UnBinding_CardNum, QString A1, QString A2)
{
    CardFlag = SwipFlag_UnBind;
    A1Card = A1;
    A2Card = A2;
    UnBingCard = UnBinding_CardNum;
}

WaitSwipCard::~WaitSwipCard()
{
    delete ui;
}

void WaitSwipCard::countdownOver()
{
    /*待完成*/
    stackLayout->setCurrentIndex(SystemSetting4Page);
    if(mt.isRunning())
    {
        protocol_exit_seek_card_status();
        mt.wait();
    }
}


